package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.BoxDashboard;
import com.example.front.entity.Dashboard;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BoxDashboardMapper {

    @Insert("INSERT INTO box_dashboard (`tenant_id`, `box_id`, `dashboard_id`) VALUES (#{tenantId}, #{boxId}, #{dashboardId})")
    void create(@Param("tenantId") long tenantId, @Param("boxId") Long boxId , @Param("dashboardId") Long dashboardId );

    @Select("<script>" +
            "SELECT * FROM `box_dashboard` " +
            "<where>" +
            " `tenant_id`=#{dashboard.tenantId} " +
            "<if test='dashboard.id!=null'> AND `dashboard_id`=#{dashboard.id}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " </script>")
    List<BoxDashboard> find(@Param("dashboard") Dashboard dashboard);

    @Select("<script>" +
            "SELECT count(*) FROM `box_dashboard` " +
            "<where>" +
            " `tenant_id`=#{dashboard.tenantId} " +
            "<if test='dashboard.id!=null'> AND `id`!=#{dashboard.id}</if>" +
            "<if test='dashboard.name!=null'> AND `name` LIKE CONCAT('%',#{dashboard.name},'%') </if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("dashboard") Dashboard dashboard);

    @Update("UPDATE `box_dashboard` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("tenantId") long tenantId, @Param("id") int id);

    @Update("UPDATE `box_dashboard` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `dashboard_id`=#{dashboardId} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteByDashboard(@Param("tenantId") long tenantId, @Param("dashboardId") Long dashboardId);

    @Select("<script>" +
            "SELECT * FROM `box_dashboard` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='dashboardIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<Dashboard> findDashboardByIds(@Param("dashboardIds") List<Long> dashboardIds);
}